﻿using Microsoft.IdentityModel.Tokens;
using SkiaSharp;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Drawing;
using System.IdentityModel.Tokens.Jwt;
using System.Reflection;
using System.Security.Claims;
using System.Security.Cryptography;
using System.Text;
using static System.Net.Mime.MediaTypeNames;

namespace VueAdminService.Utils
{
	public class EnumUtil
	{		
		/// <summary>
		///获取枚举Description中显示内容
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <returns></returns>
		public static IList<KeyValuePair<string, string>> GetEnumDisplayNames<T>()
		{
			Type type = typeof(T);
			List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();
			foreach (string name in Enum.GetNames(type))
			{
				DescriptionAttribute customAttribute = type.GetField(name).GetCustomAttribute<DescriptionAttribute>();
				if (customAttribute == null)
				{
					list.Add(new KeyValuePair<string, string>(name, name));
				}
				else
				{
					list.Add(new KeyValuePair<string, string>(name, customAttribute.Description));
				}
			}
			return list;
		}
		/// <summary>
		/// 获取枚举描述
		/// </summary>
		/// <param name="enumValue"></param>
		/// <returns></returns>
		public static string GetEnumDescription(Enum enumValue)
		{
			string value = enumValue.ToString();
			FieldInfo field = enumValue.GetType().GetField(value);
			object[] objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false);    //获取描述属性
			if (objs == null || objs.Length == 0)    //当描述属性没有时，直接返回名称
				return value;
			DescriptionAttribute descriptionAttribute = (DescriptionAttribute)objs[0];
			return descriptionAttribute.Description;
		}

	}
}
